<template>
    <div class="prospeci">
        <div v-if="!dialogTableVisible">
            <el-form label-width="80px" class="paramsset" :model="form" ref="form">
                <el-form-item label="规格一">
                    <el-input style="width: 240px;height: 28px;" placeholder="请输入规格一（请至少输入一组规格）"
                              v-model="form.specificationFirst.name"></el-input>
                    <br>
                    <el-input style="width: 100px;height: 28px;margin-right: 32px"
                              v-model="item.value" v-for="(item,index) in form.specificationFirst.specis"
                              :key="index"></el-input>
                </el-form-item>
                <el-form-item label="规格二">
                    <el-input style="width: 240px;height: 28px;" placeholder="请输入规格二"
                              v-model="form.specificationSecond.name"></el-input>
                    <br>
                    <el-input style="width: 100px;height: 28px;margin-right: 32px"
                              v-model="item.value" v-for="(item,index) in form.specificationSecond.specis"
                              :key="index"></el-input>
                </el-form-item>
                <el-form-item label="规格三">
                    <el-input style="width: 240px;height: 28px;" placeholder="请输入规格三"
                              v-model="form.specificationThird.name"></el-input>
                    <br>
                    <el-input style="width: 100px;height: 28px;margin-right: 32px"
                              v-model="item.value" v-for="(item,index) in form.specificationThird.specis" :key="index"
                    ></el-input>
                </el-form-item>
                <el-form-item label="规格四">
                    <el-input style="width: 240px;height: 28px;" placeholder="请输入规格四"
                              v-model="form.specificationFourth.name"></el-input>
                    <br>
                    <el-input style="width: 100px;height: 28px;margin-right: 32px"
                              v-model="item.value" v-for="(item,index) in form.specificationFourth.specis" :key="index"
                    ></el-input>
                </el-form-item>
                <el-form-item label="规格五">
                    <el-input style="width: 240px;height: 28px;" placeholder="请输入规格五"
                              v-model="form.specificationFifth.name"></el-input>
                    <br>
                    <el-input style="width: 100px;height: 28px;margin-right: 32px"
                              v-model="item.value" v-for="(item,index) in form.specificationFifth.specis"
                              :key="index"></el-input>
                </el-form-item>

            </el-form>

            <div class="editBtn">
                <el-button @click="goLast('pro-params')">上一步</el-button>
                <el-button @click="setRent('form')" v-if="!dialogTableVisible">下一步</el-button>

            </div>
        </div>
        <div v-if="dialogTableVisible">

            <choose-speci :speci-groups="speciGroups" @add-speci="addSpeci" :normal="normal">
            </choose-speci>

            <!--批量设置按钮-->
            <el-button type="primary" size="large" @click="handleSetRent(selectedItems)" style="margin-bottom: 20px;">
                批量设置
            </el-button>
            <el-button size="large" @click="deleteSetRent(selectedItems)" style="margin-left: 20px;">
                批量删除
            </el-button>
            <el-dialog title="将以上商品的租金设置为" :visible.sync="batchSet" custom-class="setRentDialog">
                将以上商品的
                <el-select v-model="currentSet" placeholder="请选择" style="margin-bottom: 20px;width: 80px;">
                    <el-option
                            v-for="item in batchSetItems"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value" :disabled="item.disable">
                    </el-option>
                </el-select>
                设置为
                <el-input v-model="batchSetVal" style="width: 150px;"></el-input>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="batchSet = false">取 消</el-button>
                    <el-button type="primary" @click="setRentAll">确 定</el-button>
                </div>
            </el-dialog>

            <el-table
                    :data="speciTable"
                    border
                    style="width:100%;" @selection-change="selectHandleChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column v-for="(item,index) in speciTitles" :label="item.specificationName" :key="index"
                                 :prop="'param'+(index+1)"
                                 width="120">
                </el-table-column>
                <el-table-column label="租期(天)" prop="leaseDay">
                </el-table-column>
                <!--<el-table-column label="原价">-->
                    <!--<template slot-scope="scope">-->
                        <!--<el-input v-model="scope.row.originalPrice"></el-input>-->
                    <!--</template>-->
                <!--</el-table-column>-->
                <el-table-column label="租金(元/天)">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.rent"></el-input>
                    </template>
                </el-table-column>
                <!--<el-table-column label="租期(天)" prop="leaseDay" v-if="isEdit && !isChanged">
                </el-table-column>
                <el-table-column label="租金(元/天)" v-if="isEdit && !isChanged">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.rent"></el-input>
                    </template>
                </el-table-column>
                <el-table-column v-if='!isEdit || isChanged' v-for="(item,index) in speciGroups"
                                 :label="item.specificationName"
                                 :key="index"
                                 :prop="'param'+(index+1)"
                                 width="120">
                </el-table-column>
                <el-table-column label="租期及租金" v-if="!isEdit || isChanged">
                    <template slot-scope="scope">
                        <div v-for="(d,i) in scope.row.cl" :key="i">
                            <div>
                                <el-checkbox v-model="scope.row.rentGroup[i].checked" style="width: 80px;">{{d}}
                                </el-checkbox>

                                <el-input v-model="scope.row.rentGroup[i].rent" class="setRentInp"></el-input>
                                元/天
                            </div>
                        </div>
                    </template>
                </el-table-column>-->
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                @click.native.prevent="deleteRow(scope.$index,speciTable)"
                                type="text"
                                size="small">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="editBtn">
                <el-button @click="setSpeci">上一步</el-button>

                <el-button @click="commitCheck" :disabled="!canpublish">提交审核</el-button>
            </div>

            <el-dialog
                    title="是否确认提交审核"
                    :visible.sync="dialogVisible"
                    size="tiny">
                <span>后台审核2个工作日通过后商品将会进入仓库中，请耐心等待！</span>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirmSummit">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>
<script>
    import chooseSpeci from '@/views/goods/tabcontent/components/chooseSpeci';
    import {
        getSpecis,
        setSpecis,
        removeSpeci,
        getHuiXianSpecis,
        setHuiXianSpecis,
        removeHuiXianSpeci
    } from '@/utils/storeSpeci';
    import {getProInfo,getAssures,removeAssures} from '@/utils/storeProInfo';
    import {postNormalPro} from '@/api/normalPublish';
    import {setProRent, getProRent, removeProRent, deleteProRentItem} from '@/utils/storeNormalRent';
    import {setItem, getItem, remoteItem, getParamDEL} from '@/utils/params';
    import {stripscript, isNumber, IntergerIncludeZero, isNumberZ,hasContain} from '@/utils/validate';
    import {showMessage} from '@/utils/message';
    import {deleteSpeciChangedPros, updateProInfo} from '@/api/goods';
    import {clearSession} from '@/utils/removeSession';
    import {removePubIds} from '@/utils/storeCategoryIds';
    import {getTab, setTab} from '@/utils/list';

    export default {
        props: {
            'isEdit': {
                type: Boolean,
                default: false
            },
            'info': {
                type: Object,
                default: () => {
                    return {}
                }
            }
        },
        components: {chooseSpeci},
        data() {
            let validator = (rule, value, callback) => {
                console.log(stripscript(value));
                if (stripscript(value) !== value) {
                    callback(new Error('包含特殊字符!'))
                }
                callback()
            };
            return {
                currentSet: 'SETRENT',
                batchSetItems: [
                    {
                        value: 'SETRENT',
                        label: '租金',
                        disable: false
                    },
                    {
                        value: 'SETPRICE',
                        label: '原价',
                        disable: true
                    }
                ],
                batchSetVal: '',//批量设置
                batchSet: false,
                selectedItems: [],//勾选上的
                dialogVisible: false,
                speciTable: [],
                dialogTableVisible: false,
                form: {
                    specificationFirst: {
                        name: '',
                        specis: [
                            {value: ''}, {value: ''}, {value: ''}, {value: ''}, {value: ''}
                        ]
                    },
                    specificationSecond: {
                        name: '',
                        specis: [
                            {value: ''}, {value: ''}, {value: ''}, {value: ''}, {value: ''}
                        ]
                    },
                    specificationThird: {
                        name: '',
                        specis: [
                            {value: ''}, {value: ''}, {value: ''}, {value: ''}, {value: ''}
                        ]
                    },
                    "specificationFourth": {
                        name: '',
                        specis: [
                            {value: ''}, {value: ''}, {value: ''}, {value: ''}, {value: ''}
                        ]
                    },
                    "specificationFifth": {
                        name: '',
                        specis: [
                            {value: ''}, {value: ''}, {value: ''}, {value: ''}, {value: ''}
                        ]
                    }
                },
                rules: {
                    specificationFirst: {
                        name: [{required: true, message: '请至少填写一组规格！'},
                            {validator: validator, trigger: 'blur'}]
                    }
                },
                speciGroups: [],
                lastEditPro: 'proParams',
                normal: 'normal',
                data: {},
                speciTitles: [],
                isChanged: false
//                标记规格是否修改的状态
            }
        },
        computed: {
            canpublish() //是否可提交
            {
                let items = this.speciTable || [];
                let checkedItems = []; //選中的租期及租金
                if (items.length) {
                    for (let item of items) {
                        if (item.rent) {
                            checkedItems.push(item);
                        }
                        // if (item.rent && item.originalPrice) {
                        //     checkedItems.push(item);
                        // }
                    }
                }

                return checkedItems.every(item => {
                    let rent = item.rent || '';
                    let price = item.originalPrice || '';
                    if (!isNumber(parseFloat(rent))) {
                        this.$message.error('请输入合法的金额');
                    }
                    return isNumber(parseFloat(rent))
                    // if (!isNumber(price)) {
                    //     this.$message.error('请输入合法的金额');
                    // }
                    //return isNumber(rent) && isNumber(price);

                });
                return false;
            }
        },
        methods: {
            save() {
//               切换
                if (this.form.specificationFirst.name == '') {
                    this.$message({
                        message: '请至少填写一种商品规格',
                        type: 'warning'
                    });
                    return;
                }

                for (let item of this.form.specificationFirst.specis) {
                    if (item.value == '') {
                        this.$message({
                            message: '请至少填写一种商品规格参数',
                            type: 'warning'
                        });
                        return;
                    } else {
                        break;
                    }
                }
                ;
            },
            commitCheck() {
                if (this.isEdit) {
//                    判断特色项目是否超过4个
                    if (JSON.parse(sessionStorage.getItem('specIds')).characteristicIds.split(',').length > 4) {
                        this.$message.error('特色服务至多勾选四个，请返回修改！');
                        return;
                    }

//                    编辑的提交 主要是看规格有没有修改
//                    租金处单独进行处理
                    let goodsInfo = {
                        goods: {}
                    };
                    goodsInfo.goods = getProInfo();
                    goodsInfo.goods.leaseDays = (getProInfo().leaseDays.map(function (item) {
                            return item.slice(0, item.length - 1)
                        }
                    )).join();
                    goodsInfo.goods.renewDays = (getProInfo().renewDays.map(function (item) {
                            return item.slice(0, item.length - 1)
                        }
                    )).join();
                    goodsInfo.goods.accidentInsurance = goodsInfo.goods.accidentInsurance * 100;
                    //旧版原价设置
                    goodsInfo.goods.originalPrice = goodsInfo.goods.originalPrice * 100;
                    let goodsIntro = {
                        goodsIntroduce: {}
                    };
                    let tmp = JSON.parse(sessionStorage.getItem('proIntro') ? sessionStorage.getItem('proIntro') : '{}');
                    if (tmp && JSON.stringify(tmp) != '{}') {
                        goodsIntro.goodsIntroduce.detail = tmp.detail;
                        goodsIntro.goodsIntroduce.imgJson = JSON.stringify(tmp.imgJson);
                    } else {
                        /*this.$message.error('请上传商品图片');
                        return*/
                        goodsIntro.goodsIntroduce.detail = this.info.introduce.detail;
                        goodsIntro.goodsIntroduce.imgJson = this.info.introduce.imgJson;
                    }

                    if (goodsIntro.goodsIntroduce.detail == '<p><br data-mce-bogus="1"></p>' || goodsIntro.goodsIntroduce.detail == '<p><br>""</p>' || goodsIntro.goodsIntroduce.detail == '' || goodsIntro.goodsIntroduce.detail == "<p>&nbsp;<br></p>") {
                        this.$message.error('请填写商品详情');
                        return;
                    }

//                    console.log(this.info.parametersGroupBeanList);
//                    console.log(getItem());
                    let goodsParams = {
                        goodsParameters: {
                            groups: getItem()

                        }
                    };
                    if (JSON.stringify(getParamDEL()) != '[]') {
                        goodsParams.goodsParameters.deleteGroupList = getParamDEL();
                    }
//如果既不存在参数   否则 就提供回显的参数
                    if (!goodsParams.goodsParameters.groups.length) {
//                        删除的参数个数大于1和回显的个数相同  就说明没有参数
                        if (goodsParams.goodsParameters.hasOwnProperty('deleteGroupList')) {
                            if (goodsParams.goodsParameters.deleteGroupList.length == this.info.parametersGroupBeanList.length) {
                                this.$message.error('请填写商品参数！');
                                return;
                            }
                        } else {
//                            console.log('执行回显的参数');
//                    参数的提交  getItem() 获取改变后的参数
                            let paramsLists = [];
                            for (let item of this.info.parametersGroupBeanList) {
                                let data = {
                                    id: item.id,
                                    name: item.name,
                                    parametersList: []
                                };
                                for (let item1 of item.parametersList) {
                                    let list = {
                                        parametersPoolId: item1.parametersPoolId,
                                        "parameterName": item1.parameterName,
                                        "parameterValue": item1.parameterValue,
                                        "id": item1.id
                                    };
                                    data.parametersList.push(list)
                                }
                                paramsLists.push(data);
                            }
                            setItem(paramsLists);
                            goodsParams.goodsParameters.groups = getItem();
                        }
                    } else {

                    }


                    let goodsSpeci = {
                        goodsSpecifications: {
                            specificationList: [],
                            "specificationJson": {
                                specificationFirst: [],
                                specificationSecond: [],
                                specificationThird: [],
                                specificationFourth: [],
                                specificationFifth: []
                            },
                            insertList: []
                        }
                    };

                    if (this.isChanged) {
//                       不同就是按照旧的方式  直接新增
                        goodsSpeci.goodsSpecifications.specificationList = [];
                        for (let key in getSpecis()) {
                            for (let k in getSpecis()[key]) {
                                if (k == 'name') {
                                    let a = {
                                        specificationName: getSpecis()[key][k],
                                        specification: key
                                    };
                                    goodsSpeci.goodsSpecifications.specificationList.push(a)
                                }
                            }

                            for (let a of this.form[key].specis) {
                                goodsSpeci.goodsSpecifications.specificationJson[key].push(a.value)
                            }
                        }
                        let leaseDaysArr = [];
                        for (let item of this.speciTable) {
                            if (item.rent && item.rent!='0.0' && item.rent!='0.00') {

                                let a = {
                                    specificationFirst: item.param1,
                                    specificationSecond: item.param2,
                                    specificationThird: item.param3,
                                    specificationFourth: item.param4,
                                    specificationFifth: item.param5,
                                    leaseDay: item.leaseDay.slice(0, item.leaseDay.length - 1) - 0,
                                    rent: item.rent * 100,
                                    // originalPrice: item.originalPrice * 100
                                };
                                goodsSpeci.goodsSpecifications.insertList.push(a)

//                                处理租期
                                if (!hasContain(leaseDaysArr, item.leaseDay.slice(0, item.leaseDay.length - 1))) {
                                    leaseDaysArr.push(item.leaseDay.slice(0, item.leaseDay.length - 1));
                                }
                            } else {
                                this.$message.error('您还有商品未设置租金');
                                return;
                            }
                        }
                        let renewDaysArr = goodsInfo.goods.renewDays.split(',');
                        for (let i = renewDaysArr.length - 1; i >= 0; i--) {
                            let item = renewDaysArr[i];
                            if (!hasContain(leaseDaysArr, item)) {
                                renewDaysArr.splice(i, 1);
                            }
                        }
                        goodsInfo.goods.leaseDays = leaseDaysArr.join();
                        goodsInfo.goods.renewDays = renewDaysArr.join();

                    } else {
//            相同就重新处理数据

                        goodsSpeci.goodsSpecifications.specificationList = [];
                        for (let key in getHuiXianSpecis()) {
                            for (let k in getHuiXianSpecis()[key]) {
                                if (k == 'name') {
                                    let a = {
                                        specificationName: getHuiXianSpecis()[key][k],
                                        specification: key
                                    };
                                    goodsSpeci.goodsSpecifications.specificationList.push(a)
                                }
                            }

                            for (let a of this.form[key].specis) {
                                goodsSpeci.goodsSpecifications.specificationJson[key].push(a.value)
                            }
                        }
                        let leaseDaysArr = [];
                        for (let item of this.speciTable) {
                            if (item.rent && item.rent!='0.0' && item.rent!='0.00') {
                                let a = {
                                    specificationFirst: item.param1,
                                    specificationSecond: item.param2,
                                    specificationThird: item.param3,
                                    specificationFourth: item.param4,
                                    specificationFifth: item.param5,
                                    leaseDay: item.leaseDay.slice(0, item.leaseDay.length - 1) - 0,
                                    rent: item.rent * 100,
                                    // originalPrice: item.originalPrice * 100
                                };
                                goodsSpeci.goodsSpecifications.insertList.push(a)
                                //                                处理租期
                                if (!hasContain(leaseDaysArr, item.leaseDay.slice(0, item.leaseDay.length - 1))) {
                                    leaseDaysArr.push(item.leaseDay.slice(0, item.leaseDay.length - 1));
                                }

                            } else {
                                this.$message.error('您还有商品未设置租金');
                                return;
                            }

                        }
                        let renewDaysArr = goodsInfo.goods.renewDays.split(',');
                        for (let i = renewDaysArr.length - 1; i >= 0; i--) {
                            let item = renewDaysArr[i];
                            if (!hasContain(leaseDaysArr, item)) {
                                renewDaysArr.splice(i, 1);
                            }
                        }
                        goodsInfo.goods.leaseDays = leaseDaysArr.join();
                        goodsInfo.goods.renewDays = renewDaysArr.join();
                    }
                    this.data = Object.assign({}, goodsInfo, goodsIntro, goodsParams, goodsSpeci, JSON.parse(sessionStorage.getItem('assurIds')), JSON.parse(sessionStorage.getItem('specIds')),getAssures());
                    this.dialogVisible = true;


                } else {
//                    发布的提交
                    let goodsInfo = {
                        goods: {}
                    };
                    goodsInfo.goods = getProInfo();
                    goodsInfo.goods.leaseDays = (getProInfo().leaseDays.map(function (item) {
                            return item.slice(0, item.length - 1)
                        }
                    )).join();

                    goodsInfo.goods.renewDays = (getProInfo().renewDays.map(function (item) {
                            return item.slice(0, item.length - 1)
                        }
                    )).join();
                    goodsInfo.goods.accidentInsurance = goodsInfo.goods.accidentInsurance * 100;
                    //旧版原价设置
                    goodsInfo.goods.originalPrice = goodsInfo.goods.originalPrice * 100;
                    let goodsIntro = {
                        goodsIntroduce: {}
                    };
                    let tmp = JSON.parse(sessionStorage.getItem('proIntro') ? sessionStorage.getItem('proIntro') : '{}');
                    if (tmp && JSON.stringify(tmp) != '{}') {
                        goodsIntro.goodsIntroduce.detail = tmp.detail;
                        goodsIntro.goodsIntroduce.imgJson = JSON.stringify(tmp.imgJson);
                    } else {
                        this.$message.error('请上传商品图片');
                        return
                    }


                    let goodsParams = {
                        goodsParameters: {
                            groups: getItem()
                        }
                    };

                    let goodsSpeci = {
                        goodsSpecifications: {
                            specificationList: [],
                            "specificationJson": {
                                specificationFirst: [],
                                specificationSecond: [],
                                specificationThird: [],
                                specificationFourth: [],
                                specificationFifth: []
                            },
                            insertList: []
                        }
                    };

                    goodsSpeci.goodsSpecifications.specificationList = [];
                    for (let key in getSpecis()) {
                        for (let k in getSpecis()[key]) {
                            if (k == 'name') {
                                let a = {
                                    specificationName: getSpecis()[key][k],
                                    specification: key
                                };
                                goodsSpeci.goodsSpecifications.specificationList.push(a)
                            }
                        }

                        for (let a of this.form[key].specis) {
                            goodsSpeci.goodsSpecifications.specificationJson[key].push(a.value)
                        }
                    }
                    let leaseDaysArr = [];
                    for (let item of this.speciTable) {
                        if (item.rent &&  item.rent!='0.0' && item.rent!='0.00' ) {
                            let a = {
                                specificationFirst: item.param1,
                                specificationSecond: item.param2,
                                specificationThird: item.param3,
                                specificationFourth: item.param4,
                                specificationFifth: item.param5,
                                leaseDay: item.leaseDay.slice(0, item.leaseDay.length - 1) - 0,
                                rent: item.rent * 100,
                                // originalPrice: item.originalPrice * 100
                            };
                            goodsSpeci.goodsSpecifications.insertList.push(a)

                            //                        处理租期
                            if (!hasContain(leaseDaysArr, item.leaseDay.slice(0, item.leaseDay.length - 1))) {
                                leaseDaysArr.push(item.leaseDay.slice(0, item.leaseDay.length - 1));
                            }
                        } else {
                            this.$message.error('您还有商品未设置租金或原价');
                            return;
                        }

                    }
                    let renewDaysArr = goodsInfo.goods.renewDays.split(',');
                    for (let i = renewDaysArr.length - 1; i >= 0; i--) {
                        let item = renewDaysArr[i];
                        if (!hasContain(leaseDaysArr, item)) {
                            renewDaysArr.splice(i, 1);
                        }
                    }
                    goodsInfo.goods.leaseDays = leaseDaysArr.join();
                    goodsInfo.goods.renewDays = renewDaysArr.join();

                    this.data = Object.assign({}, goodsInfo, goodsIntro, goodsParams, goodsSpeci, JSON.parse(sessionStorage.getItem('assurIds')), JSON.parse(sessionStorage.getItem('specIds')),getAssures());

                    this.dialogVisible = true;
                }

            },
            confirmSummit() {
                this.dialogVisible = false;
//                验证
//                console.log(this.data);
                for (let key in this.data) {
                    if (key == 'goodsIntroduce') {
                        for (let goodsIntroduceKey in this.data[key]) {
                            if (!this.data[key][goodsIntroduceKey]) {
                                this.$message({
                                    showClose: true,
                                    message: '商品介绍处有信息未填写完整!',
                                    type: 'error'
                                });
                                return false;
                            }
                        }
                    }
                    if (key == 'goodsParameters') {
                        for (let goodsParametersKey in this.data[key]) {
                            if (!this.data[key][goodsParametersKey]) {
                                this.$message({
                                    showClose: true,
                                    message: '商品参数处有信息未填写完整!',
                                    type: 'error'
                                });
                                return false
                            }
                        }
                    }
                    if (key == 'goodsSpecifications') {

                    }
                }

                if (this.isEdit) {
//                    确认修改
                    updateProInfo(this.data)
                        .then((resp) => {
                            showMessage({
                                type: 'success',
                                title: '商品修改成功',
                                message: ''
                            });
                            setTab('product-status-check-pending');
                            this.$emit('edit-success');
                        }).catch((err) => {
                        this.$message.error('请求超时，请稍后再试！');
                    })
                } else {
                    //                确认发布
                    postNormalPro(this.data)
                        .then((resp) => {
                            showMessage({
                                type: 'success',
                                title: '商品发布成功',
                                message: ''
                            });
                            clearSession();
                            setTab('product-status-check-pending');
                            this.$router.push({name: 'goodsList'});
                            removePubIds();
                        }).catch((err) => {
//                        发布失败
                        this.$message.error('请求超时，请稍后再试！');
                    })
                }
            }
            ,
            resetForm() {
                this.form = {};
            }
            ,
            goLast(name) {
//                回到参数
                this.$emit("nextprew", name);
            }
            ,
            setSpeci() {
//返回设置规格
                this.dialogTableVisible = false;
                this.form = getSpecis();
                setProRent(this.speciTable);


            }
            ,
            setRent() {
//                检验 下一步之前
                if (this.form.specificationFirst.name == '') {
                    this.$message({
                        message: '请至少填写一种商品规格',
                        type: 'warning'
                    });
                    return;
                }

                for (let item of this.form.specificationFirst.specis) {
                    if (item.value == '') {
                        this.$message({
                            message: '请至少填写一种商品规格参数',
                            type: 'warning'
                        });
                        return;
                    } else {
                        break;
                    }
                }
                ;

                if (this.isEdit) {
//                    用当前的与最初的比较
                    if (JSON.stringify(this.form) !== JSON.stringify(getHuiXianSpecis())) {
//                        规格变了 看租赁类型有没有变
                        this.$confirm('规格修改后，已添加商品将会被全部删除，请谨慎修改!!！', '确认修改规格？', {
                            confirmButtonText: '继续修改',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            this.isChanged = true;
//                        标记已经修改了规格
                            this.dialogTableVisible = true;
                            setSpecis(this.form);
//                            清空table
                            this.speciTable = [];
                            this.speciTitles = [];
//                            设置表格名称
                            for (let key in this.form) {
                                if (this.form[key].name !== '') {
                                    let name = {
                                        specificationName: this.form[key].name
                                    };
                                    this.speciTitles.push(name);
                                }
                            }
//                            console.log(this.speciTitles);
//设置speciGroups
                            this.speciGroups = [];
//                我每次点下一步都要把数组清空重新push
//                下一步生成speciGroups
                            for (let key in this.form) {
                                if (this.form[key].name !== '') {
                                    let item = {
                                        specification: key,
                                        specificationJson: [],
                                        specificationName: this.form[key].name
                                    };
                                    for (let a of this.form[key].specis) {
                                        if (a.value !== '') {
                                            item.specificationJson.push(a.value);
                                        }
                                    }
                                    this.speciGroups.push(item);
                                }
                                for (let i = 0; i < this.speciGroups.length; i++) {
                                    if (this.speciGroups[i].specificationJson.length == 0) {
                                        this.speciGroups.splice(i, 1)
                                    }
                                }
                            }


                        }).catch(() => {
//取消修改 那就不保存这个this.form
//                            console.log('取消了修改');
                            this.form = getHuiXianSpecis();

                        })
                    } else {
                        //                        规格相同
                        //                    执行编辑   规格改变或者是租赁类型改变都不要之前的商品了
//                        console.log(this.info.goods.leaseType)
//                        console.log(getProInfo().leaseType)
                        if (JSON.stringify(getProInfo()) == '{}') {
                            this.$message.error('商品信息处未正确填写');
                            return;
                        }
                        if (this.info.goods.leaseType != getProInfo().leaseType) {
                            this.$message({
                                type: 'warning',
                                message: '您的租赁类型已改变，请重新添加商品'
                            });
                            removeProRent();
                            this.speciTable = [];
                            this.dialogTableVisible = true;
                            this.isChanged = true;
                            setSpecis(this.form);
                            this.speciTitles = [];
//                            设置表格名称
                            for (let key in this.form) {
                                if (this.form[key].name !== '') {
                                    let name = {
                                        specificationName: this.form[key].name
                                    };
                                    this.speciTitles.push(name);
                                }
                            }
//                            console.log(this.speciTitles);
//设置speciGroups
                            this.speciGroups = [];
//                我每次点下一步都要把数组清空重新push
//                下一步生成speciGroups
                            for (let key in this.form) {
                                if (this.form[key].name !== '') {
                                    let item = {
                                        specification: key,
                                        specificationJson: [],
                                        specificationName: this.form[key].name
                                    };
                                    for (let a of this.form[key].specis) {
                                        if (a.value !== '') {
                                            item.specificationJson.push(a.value);
                                        }
                                    }
                                    this.speciGroups.push(item);
                                }
                                for (let i = 0; i < this.speciGroups.length; i++) {
                                    if (this.speciGroups[i].specificationJson.length == 0) {
                                        this.speciGroups.splice(i, 1);
                                    }
                                }
                            }

                        } else {
                            setSpecis(this.form);
//相同且 租赁类型没变
                            this.dialogTableVisible = true;
//                        获取后台数据列表
//                        获取名字titles
                            this.speciTitles = [];
                            for (let item of this.info.goodsSpecificationList) {
//                                console.log(item.specificationName);
                                if (item.specification == 'specificationFirst') {
                                    this.speciTitles[0] = {specificationName: item.specificationName}
                                }
                                if (item.specification == 'specificationSecond') {
                                    this.speciTitles[1] = {specificationName: item.specificationName}
                                }
                                if (item.specification == 'specificationThird') {
                                    this.speciTitles[2] = {specificationName: item.specificationName}
                                }
                                if (item.specification == 'specificationFourth') {
                                    this.speciTitles[3] = {specificationName: item.specificationName}
                                }
                                if (item.specification == 'specificationFifth') {
                                    this.speciTitles[4] = {specificationName: item.specificationName}
                                }
                            }
//                            console.log(this.speciTitles);
                            this.speciTitles = this.speciTitles.filter(item => {
                                return item.specificationName != '';
                            });
//                        传递给choosespeci的参数
                            this.speciGroups = [];
//                我每次点下一步都要把数组清空重新push
//                下一步生成speciGroups
                            for (let key in this.form) {
                                if (this.form[key].name !== '') {
                                    let item = {
                                        specification: key,
                                        specificationJson: [],
                                        specificationName: this.form[key].name
                                    };
                                    for (let a of this.form[key].specis) {
                                        if (a.value !== '') {
                                            item.specificationJson.push(a.value);
                                        }
                                    }
                                    this.speciGroups.push(item);
                                }
                                for (let i = 0; i < this.speciGroups.length; i++) {
                                    if (this.speciGroups[i].specificationJson.length == 0) {
                                        this.speciGroups.splice(i, 1)
                                    }
                                }
                            }
//                        渲染后台传过来 表格需要的数据
                            this.speciTable = [];
                            if (this.info.specificationCombinationList.length != 0) {
                                for (let item of this.info.specificationCombinationList) {
                                    let data = {
                                        "param1": item.specificationFirst ? item.specificationFirst : '',
                                        "param2": item.specificationSecond ? item.specificationSecond : '',
                                        "param3": item.specificationThird ? item.specificationThird : '',
                                        "param4": item.specificationFourth ? item.specificationFourth : '',
                                        "param5": item.specificationFifth ? item.specificationFifth : '',
                                        leaseDay: item.leaseDay ? item.leaseDay + '天' : '',
                                        rent: item.rent ? item.rent / 100 : 0,
                                        id: item.id,
                                        // originalPrice: isNaN(item.originalPrice / 100)?0:item.originalPrice / 100,
                                    };
                                    this.speciTable.push(data)
//                        租金
                                }
                            }
                            setProRent(this.speciTable);
                        }
                    }
                }
                else {
//                    发布
//                    如果租赁类型改变，则添加的商品清空；
                    this.dialogTableVisible = true;
                    setSpecis(this.form);
//                    console.log(this.form);
                    this.speciGroups = [];
                    this.speciTitles = [];
//                            设置表格名称
                    for (let key in this.form) {
                        if (this.form[key].name !== '') {
                            let name = {
                                specificationName: this.form[key].name
                            };
                            this.speciTitles.push(name);
                        }
                    }
//                我每次点下一步都要把数组清空重新push
//                下一步生成speciGroups
                    for (let key in this.form) {
                        if (this.form[key].name !== '') {
                            let item = {
                                specification: key,
                                specificationJson: [],
                                specificationName: this.form[key].name
                            };
                            for (let a of this.form[key].specis) {
                                if (a.value !== '') {
                                    item.specificationJson.push(a.value);
                                }
                            }
                            this.speciGroups.push(item);
                        }
                        for (let i = 0; i < this.speciGroups.length; i++) {
                            if (this.speciGroups[i].specificationJson.length == 0) {
                                this.speciGroups.splice(i, 1);
                            }
                        }
                    }
                }
//                下一步
//                console.log(this.speciGroups);
            }
            ,
            addSpeci(value) {
                //                console.log(value);
                /*if (this.isEdit) {*/
//                    编辑
                let array=[];
                let results = new Array();　 //创建一个数组
                for (let item of this.speciGroups) {
                    array.push(item.specificationJson);
                }
//                console.log(array);
                let len = array.length;
                let indexs = {};

                function GetResults(start) {
                    start++;
                    if (start > len - 1) {
                        return;
                    }
                    if (!indexs[start]) {
                        indexs[start] = 0;
                    }
                    if (!(array[start] instanceof Array)) {
                        array[start] = [array[start]];
                    }
                    for (indexs[start] = 0; indexs[start] < array[start].length; indexs[start]++) {
                        GetResults(start);
                        if (start == len - 1) {
                            var temp = [];
                            for (var i = len - 1; i >= 0; i--) {
                                if (!(array[start - i] instanceof Array)) {
                                    array[start - i] = [array[start - i]];
                                }
                                temp.push(array[start - i][indexs[start - i]]);
                            }
                            results.push(temp);
                        }
                    }
//                    console.log(results);
                    return results;
                }
                let sku=GetResults(-1);
//                console.log(sku);
                for(let i=0;i<value.length;i++){
                    if(value[i]){
                        for(let item of sku){
                            item[i]=value[i];
                        }
                    }

                }
//                console.log(sku);
//                去重
                Array.prototype.unique3 = function(){
                    var res = [];
                    var json = {};
                    for(var i = 0; i < this.length; i++){
                        if(!json[this[i]]){
                            res.push(this[i]);
                            json[this[i]] = 1;
                        }
                    }
                    return res;
                };
                sku=sku.unique3();
                // console.log(sku.unique3());
                //拿到一组数据就push进去
                let cl = getProInfo().leaseDays;
//                        console.log(cl);//7
                for(let item of sku){
                    for (let i = 0; i < cl.length; i++) {
                        let data =
                            {
                                param1: item[0] ? item[0] : '',
                                param2: item[1] ? item[1] : '',
                                param3: item[2] ? item[2] : '',
                                param4: item[3] ? item[3] : '',
                                param5: item[4] ? item[4] : '',
                                leaseDay: cl[i],
                                rent: '',
                                // originalPrice: '',
                            };
                        let a = true;
//                        假设都不一样
                        for (let i = this.speciTable.length - 1; i >= 0; i--) {
                            let item1 = this.speciTable[i];
                            if (data.param1 == item1.param1 && data.param2 == item1.param2 && data.param3 == item1.param3 && data.param4 == item1.param4 && data.param5 == item1.param5 && data.leaseDay == item1.leaseDay) {
                                this.$message.error('已经存在相同规格和租期的商品，请勿重复添加！！！');
                                a = false;
                            }
                        }
                        //没有相同的 就添加
                        if (a) {
                            this.speciTable.push(data);
                        }

                    }
                }

            }
            ,
            deleteRow(index, rows) {
                rows.splice(index, 1);
            }
            ,
            renderData() {
                for (let item of this.info.goodsSpecificationList) {
                    for (let key in this.form) {
                        if (key == item.specification) {
                            this.form[key].name = item.specificationName;
                            this.form[key].specis = [];
                            for (let items of item.specificationJson) {
                                let a = {
                                    value: items
                                };
                                this.form[key].specis.push(a);
                            }
//                            console.log(this.form[key].specis.length);
                            if (this.form[key].specis.length != 5) {
                                let num = 5 - this.form[key].specis.length;
                                for (let i = 0; i < num; i++) {
                                    this.form[key].specis.push({value: ''})
                                }
                            }
                        }
                    }
                }
            },
            selectHandleChange(val) {
//                console.log(val);
                let arr = val;
                let indexs = [];
                for (let i = 0; i < arr.length; i++) {
                    let data = arr[i];
                    for (let j = 0; j < this.speciTable.length; j++) {
                        let item = this.speciTable[j];
                        if (data.param1 == item.param1 && data.param2 == item.param2 && data.param3 == item.param3 && data.param4 == item.param4 && data.param5 == item.param5 && data.leaseDay == item.leaseDay) {
//                           相同
                            indexs.push(j);
                        }
                    }
                }
                this.selectedItems = indexs;
//                console.log(indexs);
            },
            handleSetRent() {
                if (this.selectedItems.length) {
                    this.batchSet = true;
                } else {
                    this.$message.error('您还没有选择需要批量设置的商品！');
                }

            },
            setRentAll() {
//                批量设置租金
                if (!isNumber(parseFloat(this.batchSetVal))) {
                    this.$message.error('请输入合法的金额');
                    return;
                }
                if (this.batchSetVal == '0.0') {
                    this.$message.error('请输入合法的金额');
                    return;
                }
                if (this.batchSetVal == '0.00') {
                    this.$message.error('请输入合法的金额');
                    return;
                }
                if (this.currentSet == 'SETRENT') {
                    for (let i = 0; i < this.selectedItems.length; i++) {
                        let index = this.selectedItems[i];
                        for (let j = 0; j < this.speciTable.length; j++) {
                            let item = this.speciTable[j];
//                            console.log(item);
                            if (index == j) {
//                           索引相同
                                item.rent = this.batchSetVal;
                            }
                        }
                    }
                }

                if (this.currentSet == 'SETPRICE') {
                    for (let i = 0; i < this.selectedItems.length; i++) {
                        let index = this.selectedItems[i];
                        for (let j = 0; j < this.speciTable.length; j++) {
                            let item = this.speciTable[j];
//                            console.log(item);
                            if (index == j) {
//                           索引相同
                                item.originalPrice = this.batchSetVal;
                            }
                        }
                    }
                }
                this.batchSet = false;
            },
            deleteSetRent(){
                if (this.selectedItems.length) {
                    console.log(this.selectedItems);
                    this.$confirm('此操作将删除选中规格的商品, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
//                        删除选中的商品

                        for (let i = this.selectedItems.length; i >=0 ; i--) {
                            let index = this.selectedItems[i];
                            console.log(index);
                            for(let j=this.speciTable.length;j>=0;j--){

                                if(j==index){
                                    console.log(j);
                                    this.speciTable.splice(j,1);
                                }
                            }
                        }
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }).catch(() => {

                    });
                } else {
                    this.$message.error('您还没有选择需要批量删除的商品！');
                }
            }
        }
        ,
        mounted() {
            if (this.isEdit) {
                this.renderData();
//                setSpecis(this.form);
                setHuiXianSpecis(this.form);
            } else {
                if (JSON.stringify(getSpecis()) != '{}') {
                    this.form = getSpecis();
                }
//         如果更改了租赁期限回到这里就清空
                this.$root.eventHub.$on('type-change', (val) => {
//                    console.log(val);
                    this.speciTable = [];
                })
            }
        }
        ,
    }
</script>

<style lang="less">
    .prospeci {
        .paramsset {
            .el-form-item__content {
                background: #eee;
                padding: 10px;
                .el-input {
                    &:first-of-type {
                        margin-bottom: 18px;
                    }
                }
            }
        }
        .el-table {
            margin-bottom: 20px;
            .el-table__body-wrapper .el-table__body td,
            th {
                border-right: 1px solid #dfe6ec;
            }

        }
    }

    .editBtn {
        display: block;
        text-align: center;
        margin-top: 24px;

    }

    .setRentInp {
        width: 50px;
        display: inline-block
    }
</style>
